
import json
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests


def ding():
    # 钉钉自定义应用的相关配置
    access_token = '机器人生成的token'
    agent_id = 'YOUR_DINGTALK_AGENT_ID'

    # 消息结构体
    message = {
        "msgtype": "text",
        "text": {
            "content": "http://127.0.0.1:8848/job/wms-test/13/allure/"
        }
    }
    # 发送消息的API地址
    url = f"https://oapi.dingtalk.com/cgi-bin/webhook/send?access_token={access_token}"
    # 发送请求
    response = requests.post(url, json.dumps(message))
    # 打印响应结果
    print(response.text)


def wechat():
    timestamp = get_sign_timestamp()[0]
    sgin = get_sign_timestamp()[1]
    # 钉钉的webhook地址
    webhook_url = f'https://oapi.dingtalk.com/robot/send?access_token=69a6d5f454e04d9e127d4513d85575ec0ebb3925a86bc8e92172d0441904afe1&timestamp= {timestamp}&sign={sgin}'
    formatted_time = time.strftime('%H:%M:%S', time.localtime(time.time()))
    # 消息结构体
    message = {
        "msgtype": "text",
        "text": {
            "content": "测试时间:{}".format(formatted_time)
        }
    }
    # 发送请求
    response = requests.post(webhook_url, json=message)
    # 打印响应结果
    print(response.text)


def get_sign_timestamp():

    timestamp = str(round(time.time() * 1000))
    secret = 'SEC1e1186c01384fc6edbc770655fb947204aa0d24a2ab07f048ab091170ec06cbb'
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return timestamp,sign








if __name__ == '__main__':
    wechat()
# print(wechat())


